﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;

namespace Web
{
    public partial class PostGrade : System.Web.UI.Page
    {
        private int editid = 0;
        private int delid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterSchool"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    GradeInfo gradeInfo = new GradeInfo();
                    gradeInfo = Grade.GetSingleInfo(editid);
                    GradeName.Text = gradeInfo.Name.ToString();
                    BindSchool(gradeInfo.SchoolId.ToString());
                }
                else
                {
                    BindSchool("");
                }
                if (delid > 0)
                {
                    if (!UserValidate.CheckLogin(this.Request, "DelSchool"))
                    {
                        Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                        Response.Redirect("login.aspx");
                        Response.End();
                    }
                    Grade.DelInfo(delid);
                    Response.Redirect("AdminSchool.aspx");
                    return;
                }
            }
        }
        protected void BindSchool(string selected)
        {
            IList<SchoolInfo> list = School.GetList();
            this.drpSchool.DataTextField = "Name";
            this.drpSchool.DataValueField = "Id";
            this.drpSchool.DataSource = list;
            this.drpSchool.DataBind();
            if (!PageValidate.IsEmpty(selected))
            {
                this.drpSchool.SelectedValue = selected;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (GradeName.Text.Trim() == "")
            {
                Utility.ShowJsAlert("年级名称不能为空", this);
                return;
            }
            GradeInfo gradeInfo = new GradeInfo();
            gradeInfo.Name = GradeName.Text.Trim();
            gradeInfo.SchoolId = int.Parse(this.drpSchool.SelectedItem.Value);
            gradeInfo.SchoolName = this.drpSchool.SelectedItem.Text;
            if (editid > 0)
            {
                gradeInfo.Id = editid;
            }
            Grade.Save(gradeInfo);
            Utility.ReloadTopPage(this);
        }
    }
}
